package com.yjhb.android.sbz.activity;

import android.content.Intent;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.BiddingUser;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.PutBidResult;
import com.yjhb.android.sbz.entity.Task;
import com.yjhb.android.sbz.mvp.bidding.PutBiddingPresenter;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.mvp.CommonView;
import com.jojo.android.zxlib.tool.ZxToast;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/8 17:40
 */
public class PutBiddingActivity extends BaseActivity<PutBiddingPresenter> implements CommonView<PutBidResult> {
    private final String TAG = "PutBiddingActivity";

    @BindView(R.id.money_et)
    EditText money_et;
    @BindView(R.id.task_name)
    TextView task_name;
    @BindView(R.id.recharge_tv)
    TextView recharge_tv;

    BiddingUser myBidding;
    private String name;
    private String money;

    Task task;

    @Override
    protected PutBiddingPresenter initPresenter() {
        return new PutBiddingPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_put_bidding;
    }

    @Override
    protected void initUI() {
        Serializable serializable = getIntent().getSerializableExtra("myinfo");
        if (serializable != null && serializable instanceof BiddingUser) {
            myBidding = (BiddingUser) serializable;
        }

    }

    @Override
    protected void initData() {
        if (myBidding!=null){
            name = myBidding.getName();
            money = myBidding.getMoney().toString();
        }
    }

    @Override
    protected void regEvent() {

    }


    @Override
    public void onEventMsg(EventMsg msg) {
        if (msg.msg == EventMsg.USER_UPDATE_OK) {
            refresh();
        }
    }

    @Override
    public void refresh() {
        if (!TextUtils.isEmpty(money)){
            recharge_tv.setText("加价");
            money_et.setHint("您已出价" + money + "元");
        } else {
            recharge_tv.setText("参与竞价");
            money_et.setHint("请输入竞价金额");
        }
        if (!TextUtils.isEmpty(name)){
            task_name.setText(name);
        }else {
            task_name.setText("未设置");
        }

    }

    @OnClick(R.id.back)
    public void back() {
        finish();
    }

    @OnClick(R.id.setting_task)
    void setting_put_task() {
        Intent intent = new Intent(this, BiddingTaskListActivity.class);
        startActivityForResult(intent, 10001);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10001 && resultCode == RESULT_OK) {
            if (data != null) {
                task = (Task) data.getSerializableExtra("selectedTask");
                if (task!=null){
                    name = task.getName();
                }
                refresh();
            }
        }
    }

    @OnClick(R.id.recharge_tv)
    public void recharge_tv() {
        if (Tool.isEditEmpty(money_et)) {
            ZxToast.showINFOBottomToast("请输入竞价金额");
            return;
        }
        if (task == null) {
            ZxToast.showINFOBottomToast("请设置竞价任务");
            return;
        }
        presenter.putMoney(new BigDecimal(money_et.getText().toString()), task.getCategoryId(),
                task.getName(), task.getId());
    }

    @Override
    public void onWait() {
        loadingDialog.show();
    }

    @Override
    public void onSuccess(PutBidResult data) {
        loadingDialog.cancel();
        EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_USER_UPDATE, null));
        if (data != null) {
            money = data.getMoney().toString();
            ZxToast.showSuccessCenterToast("参与成功！您已投入" + data.getMoney() + "元。");
            refresh();
            finish();
        }
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        if (code != -1)
            ZxToast.showFAILBottomToast(msg);

    }
}
